package com.kamikaze;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
    public LeaveServlet() {
        super();

    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
		
		String user_id = request.getParameter("user_id"); 
		String leaved_date_begin = request.getParameter("leaved_date_begin"); 
		String leaved_date_end = request.getParameter("leaved_date_end"); 
		String leaved_time_begin = request.getParameter("leaved_time_begin"); 
		String leaved_time_end = request.getParameter("leaved_time_end"); 
		String leave_number = request.getParameter("leave_number"); 
		String checker_id = request.getParameter("checker_id"); 
		String leave_note = request.getParameter("leave_note"); 
		Leave leave = new Leave();
		HttpSession session = request.getSession();
//取shift_number
		int shift_number = leave.getShift_number(user_id, leaved_date_begin);
		System.out.println("servlet shift_number:"+shift_number);
		if(shift_number == 0){ 
			session.setAttribute("leave", leave);
			response.sendRedirect("leave.jsp"); 
		}
//檢查請假日期格式
		boolean pass = leave.check(shift_number,leaved_date_begin,leaved_date_end);
		if(pass == true){
//將請假資料寫入資料庫		
			boolean succ = leave.process
					(user_id, leaved_date_begin, leaved_date_end, leaved_time_begin, leaved_time_end, leave_number, checker_id, leave_note, shift_number);
			if(succ){
				session.setAttribute("leave", leave);
					response.sendRedirect("leave.jsp");
		}
		else{
//回到請假頁面並帶回SESSION
			session.setAttribute("leave", leave);
			response.sendRedirect("leave.jsp");
		}
		}
	}

}
